package com.itboye.sunsun.luntan;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.sunsun.R;
import com.itboye.sunsun.adapter.LunTanDetailAdapter;
import com.itboye.sunsun.beans.LunTanHomePageBean;
import com.itboye.sunsun.beans.LunTanHomePageBean.LunTanItemBean;
import com.itboye.sunsun.beans.LuntanForumBean;
import com.itboye.sunsun.beans.LuntanForumBean.LuntanForumItemBean;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.luntan.tabadapters.PagerAdapter;
import com.itboye.sunsun.luntan.tabadapters.ScrollTabsAdapter;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

public class LunTanDetailActivity extends BaseActivity implements
		OnClickListener {

	XListView xlistview;
	TextView title;
	String fid;
	private int pageNum = 1;
	ImageView back, edit;
	BaseAdapter adapter;
	String subforum;
	List<LunTanItemBean> dataList = new ArrayList<>();
	private ScrollTabView tabs;
	private ScrollTabsAdapter tabsAdapter;
	private ViewPager viewpager;
	protected ArrayList<LuntanForumItemBean> arrayListForm;
	FrameLayout in;

	public static String ffid;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle arg0) {

		super.onCreate(arg0);

		setContentView(R.layout.activity_luntan_homepage);
		setStatusBarColor(R.color.top_blue);
		arrayListForm=new ArrayList<LuntanForumBean.LuntanForumItemBean>();
		fid = getIntent().getStringExtra("fid");
		title.setText(getIntent().getStringExtra("title"));
		subforum = getIntent().getStringExtra("subforum");
		if (subforum != null) {
			if (Integer.parseInt(subforum) <= 0) {
				tabs.setVisibility(View.GONE);
				viewpager.setVisibility(View.GONE);
				in.setVisibility(View.VISIBLE);
				// 创建修改实例
				Fragment newFragment = new MyFragment(getIntent().getStringExtra("title"), fid, 1);
				FragmentTransaction transaction = getSupportFragmentManager()
						.beginTransaction();
				transaction.replace(R.id.in, newFragment);
				transaction.commit();
			} else {
				getForum();
			}
		}else{
			tabs.setVisibility(View.GONE);
			viewpager.setVisibility(View.GONE);
			in.setVisibility(View.VISIBLE);
			// 创建修改实例
			Fragment newFragment = new MyFragment(getIntent().getStringExtra("title"), fid, 1);
			FragmentTransaction transaction = getSupportFragmentManager()
					.beginTransaction();
			transaction.replace(R.id.in, newFragment);
			transaction.commit();
		}
	}

	@SuppressWarnings("deprecation")
	void initTabs() {
		MyFragment f1 = null;
		viewpager = (ViewPager) findViewById(R.id.viewpager);
		PagerAdapter pagerAdapter = new PagerAdapter(
				getSupportFragmentManager());
		ArrayList<Integer> arrayList = new ArrayList<Integer>();
		tabsAdapter = new ScrollTabsAdapter(this, arrayList);
		for (int i = 1; i <= arrayListForm.size(); i++) {
//			arrayList.add(R.drawable.tab + i);
			tabsAdapter.add(arrayListForm.get(i - 1).getName());
			f1 = new MyFragment(getIntent().getStringExtra("title"),
					arrayListForm.get(i - 1).getFid());
			pagerAdapter.addFragment(f1);
		}
		viewpager.setAdapter(pagerAdapter);
		viewpager.setCurrentItem(0);
		viewpager.addOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				ffid = arrayListForm.get(arg0).getFid();
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});

		tabs.setViewPager(viewpager);
		tabs.setAdapter(tabsAdapter);
	}

	private void getForum() {
		MyJsonRequest.Builder<LuntanForumBean> builder = new MyJsonRequest.Builder<LuntanForumBean>()
				.typeKey("BY_ForumForum_query").apiVer("100").param("fup", fid)
				.requestListener(new XRequestListener<LuntanForumBean>() {

					@Override
					public void onResponse(LuntanForumBean orderBean) {//
						if (orderBean.getList() == null
								|| orderBean.getList().equals("null")) {
							return;
						} else {
							System.out.println(arrayListForm.size()+">>");
							arrayListForm = orderBean.getList();
							ffid = arrayListForm.get(0).getFid();
							initTabs();
						}
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
//						Toast.makeText(LunTanDetailActivity.this, "msg" + msg,
//								0).show();
					}
				});
		MyJsonRequest<LuntanForumBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		pullDown();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.edit:
			Intent intent = new Intent(this, FaBiaoTieZiActivity.class);

			intent.putExtra("fid", ffid);

			startActivity(intent);
		default:
			break;
		}

	}

	private void pullDown() {
		pageNum = 1;
		MyJsonRequest.Builder<LunTanHomePageBean> builder = new MyJsonRequest.Builder<LunTanHomePageBean>()
				.param("fid", fid).apiVer("100").param("page_no", "" + pageNum)
				.typeKey("BY_ForumThread_query").param("stick", 0)
				.requestListener(new XRequestListener<LunTanHomePageBean>() {

					@Override
					public void onResponse(LunTanHomePageBean orderBean) {
						if (adapter == null) {
							adapter = new LunTanDetailAdapter(dataList,
									LunTanDetailActivity.this);
							xlistview.setAdapter(adapter);

						}

						if (orderBean.getCount() > 0) {
							dataList.clear();
							dataList.addAll(orderBean.getList());
							adapter.notifyDataSetChanged();
						}

						pageNum++;
						xlistview.stopRefresh();
						xlistview.stopLoadMore();

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						xlistview.stopRefresh();
						xlistview.stopRefresh();
						// onPullDownError(adapter, xlistview, exception, code,
						// msg);
					}
				});

		MyJsonRequest<LunTanHomePageBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}

	protected void pullUp() {
		MyJsonRequest.Builder<LunTanHomePageBean> builder = new MyJsonRequest.Builder<LunTanHomePageBean>()
				.param("fid", fid).apiVer("100").param("page_no", "" + pageNum)
				.param("stick", 0).typeKey("BY_ForumThread_query")
				.requestListener(new XRequestListener<LunTanHomePageBean>() {

					@Override
					public void onResponse(LunTanHomePageBean orderBean) {
						// onPullUpFinish(adapter, xlistview, orderBean);
						if (adapter == null) {
							adapter = new LunTanDetailAdapter(dataList,
									LunTanDetailActivity.this);
							xlistview.setAdapter(adapter);

						}

						if (orderBean.getCount() > 0) {
							dataList.addAll(orderBean.getList());
							adapter.notifyDataSetChanged();
						}

						pageNum++;
						xlistview.stopLoadMore();
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						xlistview.stopLoadMore();
						// onPullUpError(adapter, xlistview, exception, code,
						// msg);
					}
				});

		MyJsonRequest<LunTanHomePageBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}
}
